package com.huawei.secure.android.common.encrypt.keystore.rsa;

import android.annotation.TargetApi;
import android.os.Build.VERSION;
import android.security.keystore.KeyGenParameterSpec.Builder;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStore.Entry;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.Signature;

public abstract class RSASignKS {
    private static final String A = "AndroidKeyStore";
    private static final int F = 2048;
    private static final String G = "SHA256withRSA/PSS";
    private static final String TAG = "RSASignKS";
    private static final String i = "";

    public static String sign(String str, String str2) {
        Exception e;
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            Log.e(TAG, "alias or content is null");
            return "";
        } else if (b()) {
            try {
                Entry j = j(str);
                if (j == null || !(j instanceof PrivateKeyEntry)) {
                    Log.e(TAG, "Not an instance of a PrivateKeyEntry");
                    return "";
                }
                Signature instance = Signature.getInstance(G);
                instance.initSign(((PrivateKeyEntry) j).getPrivateKey());
                instance.update(str2.getBytes("UTF-8"));
                return Base64.encodeToString(instance.sign(), 0);
            } catch (GeneralSecurityException e2) {
                e = e2;
                Log.e(TAG, "encrypt exception : " + e.getMessage());
                return "";
            } catch (UnsupportedEncodingException e3) {
                e = e3;
                Log.e(TAG, "encrypt exception : " + e.getMessage());
                return "";
            }
        } else {
            Log.e(TAG, "sdk version is too low");
            return "";
        }
    }

    public static boolean verifySign(String str, String str2, String str3) {
        boolean verify;
        Exception e;
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3)) {
            Log.e(TAG, "alias or content or sign value is null");
            return false;
        } else if (b()) {
            try {
                Entry j = j(str);
                if (j == null || !(j instanceof PrivateKeyEntry)) {
                    Log.e(TAG, "Not an instance of a PrivateKeyEntry");
                    return false;
                }
                Signature instance = Signature.getInstance(G);
                instance.initVerify(((PrivateKeyEntry) j).getCertificate());
                instance.update(str2.getBytes("UTF-8"));
                verify = instance.verify(Base64.decode(str3, 0));
                return verify;
            } catch (GeneralSecurityException e2) {
                e = e2;
                Log.e(TAG, "verify sign exception : " + e.getMessage());
                verify = false;
                return verify;
            } catch (UnsupportedEncodingException e3) {
                e = e3;
                Log.e(TAG, "verify sign exception : " + e.getMessage());
                verify = false;
                return verify;
            }
        } else {
            Log.e(TAG, "sdk version is too low");
            return false;
        }
    }

    @TargetApi(23)
    private static KeyPair f(String str) {
        KeyPair keyPair = null;
        if (i(str)) {
            Log.e(TAG, "Key pair exits");
        } else {
            try {
                KeyPairGenerator instance = KeyPairGenerator.getInstance("RSA", A);
                instance.initialize(new Builder(str, 12).setDigests(new String[]{AaidIdConstant.SIGNATURE_SHA256, "SHA-512"}).setSignaturePaddings(new String[]{"PSS"}).setKeySize(2048).build());
                keyPair = instance.generateKeyPair();
            } catch (GeneralSecurityException e) {
                Log.e(TAG, "generate keypair exception: " + e.getMessage());
            }
        }
        return keyPair;
    }

    private static Entry j(String str) {
        Exception e;
        if (!i(str)) {
            f(str);
        }
        try {
            KeyStore instance = KeyStore.getInstance(A);
            instance.load(null);
            return instance.getEntry(str, null);
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "load entry exception : " + e.getMessage());
            return null;
        } catch (IOException e3) {
            e = e3;
            Log.e(TAG, "load entry exception : " + e.getMessage());
            return null;
        }
    }

    private static boolean b() {
        return VERSION.SDK_INT >= 23;
    }

    private static boolean i(String str) {
        Exception e;
        try {
            KeyStore instance = KeyStore.getInstance(A);
            instance.load(null);
            if (instance.getKey(str, null) != null) {
                return true;
            }
            return false;
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "key pair exists exciption : " + e.getMessage());
            return false;
        } catch (IOException e3) {
            e = e3;
            Log.e(TAG, "key pair exists exciption : " + e.getMessage());
            return false;
        }
    }
}
